package com.ruoyi.genpdf.service;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.ruoyi.genpdf.domain.LotteryTicketData;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

@Service
public class AdvancedLotteryPdfService {

    public Resource generateCustomLotteryTicket(LotteryTicketData data) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        PdfWriter writer = new PdfWriter(outputStream);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf, PageSize.A6);

        // 设置字体
        PdfFont font = getChineseFont();
        document.setFont(font);

        // 构建PDF内容
        buildPdfContent(document, data);

        document.close();

        return new ByteArrayResource(outputStream.toByteArray());
    }

    private void buildPdfContent(Document document, LotteryTicketData data) {
        // 标题
        addCenteredText(document, "玩法：" + data.getPlayMethod(), 14, true);

        // 机号
        addLeftText(document, "机号：" + data.getMachineNumber(), 10);

        // 代码
        addCenteredText(document, data.getCode(), 8, false);

        // 分隔线
        addSeparator(document);

        // 投注信息
        addLeftText(document, "组三复式投注：合计", 12, true);
        addCenteredText(document, data.getBetCount() + " 注", 12, true);
        addLeftText(document, "注释：" + data.getComment(), 10);
        addLeftText(document, "倍数：" + data.getMultiple(), 10);

        addSeparator(document);

        // 表格
        Table table = createInfoTable(data);
        document.add(table);

        // 地址和公益信息
        addLeftText(document, "地址：" + data.getAddress(), 8);
        addCenteredText(document, "感谢您为公益事业贡献 " + data.getDonation() + " 元", 8,true);
    }

    private Table createInfoTable(LotteryTicketData data) {
        float[] columnWidths = {150, 150};
        Table table = new Table(UnitValue.createPercentArray(columnWidths));

        table.addCell(createCell("开奖期：", 8, false));
        table.addCell(createCell(data.getDrawPeriod(), 8, false));

        table.addCell(createCell("销售期：", 8, false));
        table.addCell(createCell(data.getSalePeriod(), 8, false));

        table.addCell(createCell("", 8, false));
        table.addCell(createCell(data.getSaleTime(), 8, false));

        table.addCell(createCell("合计：" + data.getTotalAmount() + "元", 10, true));

        return table;
    }

    private void addCenteredText(Document document, String text, int fontSize, boolean bold) {
        Paragraph paragraph = new Paragraph(text).setFontSize(fontSize);
        if (bold) paragraph.setBold();
        paragraph.setTextAlignment(TextAlignment.CENTER);
        document.add(paragraph);
    }

    private void addLeftText(Document document, String text, int fontSize) {
        addLeftText(document, text, fontSize, false);
    }

    private void addLeftText(Document document, String text, int fontSize, boolean bold) {
        Paragraph paragraph = new Paragraph(text).setFontSize(fontSize);
        if (bold) paragraph.setBold();
        document.add(paragraph);
    }

    private void addSeparator(Document document) {
        document.add(new Paragraph("----------------------------------------")
                .setFontSize(8)
                .setTextAlignment(TextAlignment.CENTER));
    }

    private PdfFont getChineseFont() throws IOException {
        try {

            return PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H");
        } catch (Exception e) {
            return PdfFontFactory.createFont();
        }
    }

    private Cell createCell(String text, int fontSize, boolean bold) {
        Paragraph paragraph = new Paragraph(text).setFontSize(fontSize);
        if (bold) paragraph.setBold();
        return new Cell().add(paragraph).setPadding(2);
    }
}

